package com.ml.mall.api.coupon;

import com.alibaba.fastjson.JSONObject;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.coupon.CouponEditDto;
import com.ml.mall.bean.req.coupon.CouponNewDto;
import com.ml.mall.bean.req.coupon.CouponQueryDto;
import com.ml.mall.bean.req.coupon.CouponQueryOneDto;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.coupon.SysCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 *   @description    :description 
 *   @author         :zxm
 *   @date           :2021/5/21
 *   @version        :1.0
**/
@Api(tags = "【product】优惠券相关接口")
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private SysCouponService sysCouponService;

    @ApiOperation("优惠券新增")
    @PostMapping("/new")
//    @RequiresPermissions(PermissionConst.COUPON_NEW)
    public ApiRestResult couponNew(@RequestBody @Valid CouponNewDto dto){
        SysUser currentUser = JwtUtil.getCurrentUser();
        if(currentUser == null){
            return ApiRestResult.err();
        }
        return sysCouponService.createCoupon(dto,currentUser);
    }

    @ApiOperation("优惠券修改")
    @PostMapping("/edit")
//    @RequiresPermissions(PermissionConst.COUPON_EDIT)
    public ApiRestResult couponEdit(@RequestBody @Valid CouponEditDto dto){
        return sysCouponService.editCoupon(dto);
    }

    @ApiOperation("优惠券删除")
    @PostMapping("/del")
//    @RequiresPermissions(PermissionConst.COUPON_DEL)
    public ApiRestResult couponDel(@RequestBody List<Long> ids){
        return sysCouponService.removeCoupon(ids);
    }

    @ApiOperation("优惠券分页查询")
    @PostMapping("/query_page")
//    @RequiresPermissions(PermissionConst.COUPON_QUERY_PAGE)
    public ApiRestResult couponQueryPage(@RequestBody CouponQueryDto dto){
        return ApiRestResult.ok(sysCouponService.queryPageCoupon(dto));
    }

    @ApiOperation("优惠券单个查询")
    @PostMapping("/query_one")
//    @RequiresPermissions(PermissionConst.COUPON_QUERY_ONE)
    public ApiRestResult couponQueryOne(@RequestBody @Valid CouponQueryOneDto dto){
        return ApiRestResult.ok(sysCouponService.queryOneCoupon(dto));
    }
}
